package com.example.controller;

import com.example.config.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class LoginController {


    @RequestMapping("/user/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        Model model, HttpSession session, HttpServletRequest httpServletRequest){
        //具体的业务;
        if(!StringUtils.isEmpty(userName) && "12345".equals(password)){
            HashMap map = new HashMap<String,String>();
            map.put("userName",userName);
            map.put("password",password);
            session.setAttribute("loginUser",map);

            return "redirect:/main.html";
        }

        //告诉用户登录失败
        model.addAttribute("msg","用户名或者密码错误");

        return "index";
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();

        return "redirect:/index.html";
    }
}
